@extends "main_not_nav.html"

@block body
<br>
<form class="layui-form">
    {{ csrf_field }}

    <div class="layui-form-item">
        <label class="layui-form-label">账户</label>
        <div class="layui-input-inline">
            <input type="text" value="{{ account }}" readonly autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label">admin_user_id</label>
        <div class="layui-input-inline">
            <input type="number" name="admin_user_id" value="{{ admin_user_id }}" readonly autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色<span class="layui-color-red">*</span></label>
        <div class="layui-input-inline">
            <select name="admin_role_id" id="admin_role_id" lay-verify="required">
                <option value="">-- 请选择 --</option>
                {% for role in roles %}
                <option value="{{ role['id'] }}">{{ role['title'] }}</option>
                {% endfor %}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

@endblock

@block js

<script>
    //Demo
    layui.use(['form', 'laydate'], function(){
        var form = layui.form;

        $("#admin_role_id").find("option[value='"+"{{ admin_role_id }}"+"']").attr('selected', true);
        form.render('select');

        //监听提交
        form.on('submit(formDemo)', function(data){
            //当前容器的全部表单字段，名值对形式：{name: value}
            // console.log(data.field);
            // layer.msg(JSON.stringify(data.field));
            $.ajax({
                url: "{{ uri }}/store",
                method: "post",
                data: data.field,
                dataType: "JSON",
                success: function (callback){
                    // console.log(callback);
                    layer.msg(callback.message);

                    if (callback.code === 1) {
                        setTimeout(function(){
                            window.parent.location.reload();
                        }, 2000);
                    }
                }
            });

            return false;
        });
    });
</script>

@endblock